<?php
/**
 * 管理组控制器
 * PHP version 5
 * @package   Antonia
 * @author    Antonia <471699715@qq.com>
 * @copyright 2016 Antonia.
 */
namespace Admin\Controller;
use Think;
class AdmingroupsController extends AdmincommonController {
    /**
     * 显示管理组列表
     * @param HttpRequest $request
     * @return html
     * @author Antonia
    */
    public function index() {
        $filters = array();
        $_POST['group_name'] = remove_xss($_POST['group_name']);
        !empty($_POST['group_name']) && $filters['group_name'] = array('like','%' . $_POST['group_name'] . '%');
        $group = D("Admingroups");
        $_GET['p'] = $_POST['p'];
        if (!empty ($_POST['orderField'] )) {
            $orderField = $_POST['orderField'];
        } else {
            $orderField = ! empty ( $sortBy ) ? $sortBy : $group->getPk();
        }
        if (isset ( $_POST['orderDirection'] )) {
            $orderDirection = $_POST['orderDirection'] == 'asc' ? 'asc' : 'desc';
        } else {
            $orderDirection = 'desc';
        }
        $count = $group->where($filters)->count();
        if (!empty($_POST['numPerPage'])){
            $numPerPage = $_POST['numPerPage'];
        } else {
            $numPerPage = 20;
        }
        $Page  =  new \Think\Page($count,$numPerPage);
        $list = $group->order('sortNum asc,' . $orderField . " " . $orderDirection)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->where($filters)
            ->select();
        $Admingroups = D("Admingroups");
        $this->assign('groups', $Admingroups->getOptions());
        $this->assign('list',$list);
        $this->assign('orderDirection', $orderDirection);
        $this->assign('orderField', $orderField);
        $this->assign('filters', $_POST);
        $this->assign('totalCount', $count);
        $this->assign('numPerPage', $Page->listRows);
        $this->assign('currentPage', !empty($_GET['p'])?$_GET['p']:1);
        $this->display();
    }

    /**
     * 批量添加
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function addAdmin() {
        $group_id = remove_xss($_GET['id']);
        if (!empty($group_id)) {
            $Sections     = D("Admingroups");
            $SectionsInfo = $Sections->find($group_id);
            if ($SectionsInfo) {
                $this->assign('id', $group_id);
                $this->display('addadmin');
            } else {
                $re['statusCode'] = "300";
                $re['navTabId'] = "Admingroupsindex";
                $re['message'] = "该记录不存在！";
                echo json_encode($re);
            }
        } else {
            $re['statusCode'] = "300";
            $re['navTabId'] = "Admingroupsindex";
            $re['message'] = "该记录不存在！";
            echo json_encode($re);
        }
    }

    /**
     * 处理批量添加
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function addAdminHandle() {
        if(!empty($_POST['id']) && !empty($_POST['admin_id'])) {
            $Sections_id = remove_xss($_POST['id']);
            $adminArr = explode(',', rtrim($_POST['admin_id'], ','));
            $Admins = D("Admins");
            foreach($adminArr as $admin_id) {
                $old_section_id = $Admins->where('admin_id=' . $admin_id)->getField('group_id');
                if($old_section_id) {
                    $old_section_arr = explode(',', trim($old_section_id, ','));
                    if(in_array($Sections_id, $old_section_arr)) {
                        continue;
                    } else {
                        $new_section_id = $old_section_id . $Sections_id . ',';
                        $Admins->where('admin_id=' . $admin_id)->setField('group_id', $new_section_id);
                    }
                } else {
                    if($old_section_id == "") {
                        $new_section_id = ',' . $old_section_id . $Sections_id . ',';
                        $Admins->where('admin_id=' . $admin_id)->setField('group_id', $new_section_id);
                    }
                }
            }
            $re['statusCode'] = "200";
            $re['message'] = "操作成功";
            echo json_encode($re);
        } else {
            $re['statusCode'] = "300";
            $re['message'] = "操作失败，请重试。";
            echo json_encode($re);
        }
    }

    /**
     * 显示新增管理组页面
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function add() {
        $Auths = D("Auths");
        $this->assign('authInfo',  $Auths->getAuths());
        $this->display('info');
    }

    /**
     * 处理新增
     *
     * @param HttpRequest $request
     *
     * @return void
     * @author Fee
    */
    public function insert() {
        $group = D("Admingroups");
        if ($vo = $group->create()) {
            unset($vo['group_id']);
            $rs = $group->add($vo);
            if ($rs !== false) {
                $action_arr = $_POST['action'];
                $Admingroupauths = D('Admingroupauths');
                $Admingroupauths->setAuths($rs, $action_arr);
				
                $re['statusCode'] = "200";
                $re['message'] = "新增成功!";
                $re['navTabId'] = "Admingroupsindex";
                $re['callbackType'] = "closeCurrent";
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "新增失败，请重试!";
            }
        } else {
			$re['statusCode'] = "300";
            $re['message'] = $group->getError();
        }
		echo json_encode($re);
    }

    /**
     * 显示编辑页面
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function edit() {
        $group_id = remove_xss($_GET['id']);
        if (!empty($group_id)) {
            $group     = D("Admingroups");
            $groupInfo = $group->find($group_id);
            if ($groupInfo) {
                $this->assign('group', $groupInfo);
                $Admingroupauths = D('Admingroupauths');
                $auths = $Admingroupauths->getAuthsByGroup($group_id);
                $Auths = D("Auths");
                $this->assign('authInfo',  $Auths->getAuths($auths));
                $this->display('info');
            } else {
				$re['statusCode'] = "300";
                $re['navTabId'] = "Admingroupsindex";
                $re['message'] = "该记录不存在！";
                echo json_encode($re);
            }
        } else {
			$re['statusCode'] = "300";
			$re['navTabId'] = "Admingroupsindex";
			$re['message'] = "该记录不存在！";
			echo json_encode($re);
        }
    }

    /**
     * 处理更新数据
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function update()
    {
        $group = D("Admingroups");
        if ($vo = $group->create()) {
            $list = $group->save($vo);
            if ($list !== false) {
                $action_arr = $_POST['action'];
                $Admingroupauths = D('Admingroupauths');
                $Admingroupauths->setAuths($vo['group_id'], $action_arr);
                $re['statusCode'] = "200";
                $re['message'] = "修改成功!";
                $re['navTabId'] = "Admingroupsindex";
            } else {
				$re['statusCode'] = "300";
                $re['message'] = "没有更新任何数据!";
            }
        } else {
			 $re['statusCode'] = "300";
             $re['message'] = $group->getError();
        }
		echo json_encode($re);
    }

    /**
     * 删除管理组
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function delete() {
        if (!empty($_GET['id'])) {
            $group = D("Admingroups");
            $result = $group->delete(remove_xss($_GET['id']));
            if (false !== $result) {
                $re['statusCode'] = "200";
                $re['message'] = "删除成功!";
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "删除失败，请重试!";
            }
        } else {
                $re['statusCode'] = "300";
                $re['message'] = "该记录不存在！";
        }
        echo json_encode($re);
    }

    /**
     * 删除选定内容
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function delAll() {
        $group = D("Admingroups");
        $map['group_id']  = array('in', explode(',', $_POST["ids"]));
        $result  = $group->where($map)->delete();
        if($result) {
            $re['statusCode'] = "200";
            $re['message'] = "删除成功!";
        } else {
            $re['statusCode'] = "300";
            $re['message'] = "删除失败，请重试!";
        }
        echo json_encode($re);
    }
}
?>